﻿using System;
using System.Windows.Forms;
using BillsPresentation;
using BillsDomain;
using System.Resources;
using System.Reflection;
using System.Globalization;
using Bills.Properties;

namespace Bills
{
    /// <summary>
    /// Interaction logic for FormAddPayment
    /// </summary>
    public partial class FormAddPayment : Form, IAddPaymentView
    {
        /// <summary>
        /// Occurs when the Save button is pressed.
        /// </summary>
        public event EventHandler SaveClicked;

        /// <summary>
        /// The form's presenter
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields",Justification="Its constructor takes the form as an argument and wires events.")]
        AddPaymentPresenter presenter;

        /// <summary>
        /// Initializes a new instance of the <see cref="FormAddPayment"/> class.
        /// </summary>
        public FormAddPayment()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets a value indicating whether the payment is a new one or an existing one.
        /// </summary>
        /// <value><c>true</c> if adding a new payment; otherwise, <c>false</c>.</value>
        public bool AddMode { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="FormAddPayment"/> class.
        /// </summary>
        /// <param name="controller">The controller.</param>
        public FormAddPayment(IScheduledPaymentController controller)
            : this()
        {
            this.presenter = new AddPaymentPresenter(this, controller);
        }

        /// <summary>
        /// Gets or sets the amount.
        /// </summary>
        /// <value>The amount.</value>
        public string Amount
        {
            get { return this.textBoxAmount.Text; }
            set { this.textBoxAmount.Text = value; }
        }

        /// <summary>
        /// Gets the recurrence lookup list.
        /// </summary>
        /// <value>The recurrence lookup.</value>
        public ILookupList RecurrenceLookup
        {
            get { return new FormLookupList (this.comboBoxRecurrenceType); }
        }

        /// <summary>
        /// Gets the payee name lookup list.
        /// </summary>
        /// <value>The payee name lookup list.</value>
        public ILookupList PayeeLookup
        {
            get { return new FormLookupList(this.comboBoxPayee); }
        }

        /// <summary>
        /// Gets the type lookup list.
        /// </summary>
        /// <value>The type lookup.</value>
        public ILookupList TypeLookup
        {
            get { return new FormLookupList (this.comboBoxType); }
        }

        /// <summary>
        /// Gets or sets the start date.
        /// </summary>
        /// <value>The start date.</value>
        public DateTime StartDate
        {
            get { return new DateTime(this.dateTimePickerNextPayment.Value.Year, this.dateTimePickerNextPayment.Value.Month, this.dateTimePickerNextPayment.Value.Day); }
            set { this.dateTimePickerNextPayment.Value = value; }
        }

        /// <summary>
        /// Handles the Click event of the buttonCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// Handles the Click event of the buttonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (this.SaveClicked != null)
            {
                this.SaveClicked(sender, e);
            }

            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// Occurs when the window is closed.
        /// </summary>
        public event EventHandler FormClose;

        /// <summary>
        /// Handles the FormClosing event of the FormAddPayment control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void FormAddPayment_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.FormClose != null)
            {
                this.FormClose(sender, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the payment should be included in balance projections.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the payment should be included in balance projections; otherwise, <c>false</c>.
        /// </value>
        public bool IncludeInProjection
        {
            get { return this.checkBoxIncludeInProjections.Checked; }
            set { this.checkBoxIncludeInProjections.Checked = value; }
        }
    }
}
